package com.project.weimi.activity;

import com.project.weimi.R;
import com.project.weimi.entity.response.AddressEntity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.NumberPicker;
import android.widget.NumberPicker.OnValueChangeListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class AddressManagerEditorActivity extends BaseActivity implements OnValueChangeListener {

	private String title = "";
	private int states = 0;
	private int position = -1;
	private int resultCode = 0;

	private AddressEntity addressEntity = null;

	private EditText address_et_name, address_et_phone, address_et_youbian, address_et_address;
	private RelativeLayout address_ssq_grp, address_jd_grp;
	private TextView address_ssq_show, address_jd_show;
	private TextView address_save;

	String[] city = { "立水桥", "霍营", "回龙观", "龙泽", "西二旗", "上地" };
	String[] sheng = { "sheng--1", "sheng--2", "sheng--3", "sheng--4", "sheng--5", "sheng--6" };
	String[] shi = { "shi--1", "shi--2", "shi--3", "shi--4", "shi--5", "shi--6" };
	String[] qu = { "qu--1", "qu--2", "qu--3", "qu--4", "qu--5", "qu--6" };
	
	int currentJD = 0;
	int currentSheng = 0;
	int currentShi = 0;
	int currentQu = 0;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		Bundle bundle = getIntent().getExtras();

		states = bundle.getInt("states");
		if (states == 1) {
			resultCode = 1;
			position = bundle.getInt("position");
			title = "编辑";
		} else if (states == 2) {
			resultCode = 2;
			title = "新增";
		}

		addressEntity = (AddressEntity) bundle.getSerializable("address");
		setContentViewWithBar(R.layout.activity_addressmanager_editor, title + "收货地址");
		initView();

		if (addressEntity == null) {
			addressEntity = new AddressEntity();
			addressEntity.setAddress_check(false);
		} else {
			showAddressInfo();
		}
	}

	private void showAddressInfo() {
		address_et_name.setText(addressEntity.getAddress_name());
		address_et_phone.setText(addressEntity.getAddress_phone());
		address_et_youbian.setText(addressEntity.getAddress_youbian());
		address_et_address.setText(addressEntity.getAddress_address());
		address_ssq_show.setText(addressEntity.getAddress_area());
		address_jd_show.setText(addressEntity.getAddress_jiedao());
	}

	private void initView() {
		address_et_name = (EditText) this.findViewById(R.id.address_et_name);
		address_et_phone = (EditText) this.findViewById(R.id.address_et_phone);
		address_et_youbian = (EditText) this.findViewById(R.id.address_et_youbian);
		address_et_address = (EditText) this.findViewById(R.id.address_et_address);
		address_ssq_grp = (RelativeLayout) this.findViewById(R.id.address_ssq_grp);
		address_jd_grp = (RelativeLayout) this.findViewById(R.id.address_jd_grp);
		address_ssq_show = (TextView) this.findViewById(R.id.address_ssq_show);
		address_jd_show = (TextView) this.findViewById(R.id.address_jd_show);
		address_save = (TextView) this.findViewById(R.id.address_save);

		address_ssq_grp.setOnClickListener(this);
		address_jd_grp.setOnClickListener(this);
		address_save.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		AlertDialog dialog = null;
		switch (v.getId()) {
		case R.id.address_ssq_grp:// 省市区
			LinearLayout linearLayout = (LinearLayout) LayoutInflater.from(AddressManagerEditorActivity.this).inflate(R.layout.activity_address_editor_area, null);
			
			NumberPicker valuepicker1 = (NumberPicker) linearLayout.findViewById(R.id.valuepicker1);
			valuepicker1.setDisplayedValues(sheng);
			valuepicker1.setMinValue(0);
			valuepicker1.setMaxValue(sheng.length - 1);
			valuepicker1.setValue(0);
			valuepicker1.setOnValueChangedListener(this);
			
			NumberPicker valuepicker2 = (NumberPicker) linearLayout.findViewById(R.id.valuepicker2);
			valuepicker2.setDisplayedValues(shi);
			valuepicker2.setMinValue(0);
			valuepicker2.setMaxValue(shi.length - 1);
			valuepicker2.setValue(0);
			valuepicker2.setOnValueChangedListener(this);
			
			NumberPicker valuepicker3 = (NumberPicker) linearLayout.findViewById(R.id.valuepicker3);
			valuepicker3.setDisplayedValues(qu);
			valuepicker3.setMinValue(0);
			valuepicker3.setMaxValue(qu.length - 1);
			valuepicker3.setValue(0);
			valuepicker3.setOnValueChangedListener(this);
			
			dialog = new AlertDialog.Builder(AddressManagerEditorActivity.this).setTitle("请选择区域")
					.setPositiveButton("确定", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							address_ssq_show.setText(sheng[currentSheng] + shi[currentShi] + qu[currentQu]);
						}
					}).setNeutralButton("取消", null).setView(linearLayout).create();
			dialog.show();
			break;
		case R.id.address_jd_grp:// 街道
			NumberPicker valuepicker = new NumberPicker(AddressManagerEditorActivity.this);
			valuepicker.setDisplayedValues(city);
			valuepicker.setMinValue(0);
			valuepicker.setMaxValue(city.length - 1);
			valuepicker.setValue(0);
			valuepicker.setId(0);
			valuepicker.setOnValueChangedListener(this);

			dialog = new AlertDialog.Builder(AddressManagerEditorActivity.this).setTitle("请选择街道")
					.setPositiveButton("确定", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							address_jd_show.setText(city[currentJD]);
						}
					}).setNeutralButton("取消", null).setView(valuepicker).create();
			dialog.show();

			break;
		case R.id.address_save:// 保存
			AddressEntity addressEntity = new AddressEntity();
			
			String name = address_et_name.getText().toString().trim();
			String phone = address_et_phone.getText().toString().trim();
			String area = address_ssq_show.getText().toString().trim();
			String jiedao = address_jd_show.getText().toString().trim();
			String youbian = address_et_youbian.getText().toString().trim();
			String address = address_et_address.getText().toString().trim();
			
			addressEntity.setAddress_name(name);
			addressEntity.setAddress_phone(phone);
			addressEntity.setAddress_area(area);
			addressEntity.setAddress_jiedao(jiedao);
			addressEntity.setAddress_youbian(youbian);
			addressEntity.setAddress_address(address);
			addressEntity.setAddress_check(this.addressEntity.isAddress_check());
			
			Intent intent = getIntent();
			Bundle bundle = new Bundle();
			bundle.putSerializable("addres", addressEntity);
			bundle.putInt("position", position); // position
			intent.putExtras(bundle);
			setResult(resultCode, intent);
			
			finish();
			break;
		default:
			break;
		}
	}

	@Override
	void setStatusBar(int statusBarColorRes) {
		// TODO Auto-generated method stub
		super.setStatusBar(R.color.transparent_3);
	}

	@Override
	public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
		// TODO Auto-generated method stub

		int id = picker.getId();
		switch (id) {
		case R.id.valuepicker1:
			currentSheng = newVal;
			break;
		case R.id.valuepicker2:
			currentShi = newVal;
			break;
		case R.id.valuepicker3:
			currentQu = newVal;
			break;
		case 0:
			currentJD = newVal;
			break;
		default:
			break;
		}
	}
}
